package me.send.business.service.mapper;

import java.util.List;
import java.util.Map;

import me.send.business.service.BaseMapper;
import me.send.business.service.pojo.BrokerInfoBean;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public abstract interface PopuserMapper extends BaseMapper {

	// 经纪人 2015年7月2日17:14:13
	@Select(value = "select * from popuser where popuserid in (select popuserid from popuser where userid = #{userid}) limit #{page.start},#{page.size}")
	public abstract List<Map<String, Object>> page_userid(
			Map<String, Object> parameter);
	
	@Insert(value = "INSERT INTO collectionuser  (userid,popuserid,createtime,updatetime) VALUES (#{userid},#{brokerid},#{createtime},#{updatetime})")
	public abstract int collect_broker(Map<String, Object> parameter);
	
	@Select(value = "SELECT COUNT(1) FROM collectionuser c WHERE c.popuserid=#{brokerid} AND userid = #{userid}")
	public abstract int isCollectedBroker(Map<String, Object> parameter);
	
	@Delete(value="DELETE FROM collectionuser WHERE popuserid=#{brokerid} AND userid = #{userid}")
	public abstract int cancelCollectBroker(Map<String, Object> parameter);
	
	@Select(" select popid,username,headerpath,sex,telephone,`level`,"
			+ "( SELECT COUNT(1) FROM orders o LEFT JOIN orderbroker ob ON o.orderid = ob.order_id WHERE ob.broker_id = #{brokerid} AND ob.ischecked =1 AND o.`status`>=8 ) as orderCount,"
			+ "( SELECT IF(COUNT(1)>0,sum(be.point)/count(be.point),0) FROM broker_evaluate be LEFT JOIN orders o ON o.orderid = be.orderid "
			+ "LEFT JOIN orderbroker ob ON ob.order_id = o.orderid WHERE ob.broker_id=#{brokerid} AND ob.ischecked=1 ) as stars,"
			+ "(SELECT IF(COUNT(1)>0,1,0) FROM collectionuser c WHERE c.userid = #{userid} AND c.popuserid = #{brokerid}) iscollection "
			+ " from popuser where popid = #{brokerid}")
	public abstract BrokerInfoBean getBrokerInfo(Map<String, Object> params);
	
	/**
	 * 获取收藏的经纪人列表
	 * @param userid
	 * @return
	 * @author maxmin
	 * @date 2015年9月2日上午10:10:24
	 */
	@Select(value="SELECT c.colluserid,p.username,p.headerpath,p.popid brokerid,p.`level`,"
			+ "(SELECT COUNT(1) FROM orders o LEFT JOIN orderbroker ob ON o.orderid = ob.order_id "
			+ "		WHERE ob.broker_id = p.popid AND ob.ischecked=1 AND o.`status`>=8) ordercount,"
			+ "(SELECT IF(COUNT(1)>0,SUM(be.point)/COUNT(be.point) ,0) "
			+ "		FROM broker_evaluate be "
			+ "		LEFT JOIN orders o ON o.orderid = be.orderid "
			+ "		LEFT JOIN orderbroker odb ON o.orderid=odb.order_id "
			+ "		LEFT JOIN popuser pp ON odb.broker_id=pp.popid  "
			+ "		WHERE pp.popid=p.popid"
			+ ") stars,p.telephone "
			+ "FROM collectionuser c LEFT JOIN popuser p ON c.popuserid = p.popid WHERE userid=#{userid} AND IF(#{startid}>0,c.colluserid>#{startid},1=1) limit #{size}")
	public abstract List<Map<String, Object>> collectionBrokerList(Map<String, Object> params);
	
	@Update(value="UPDATE popuser SET level=#{level} WHERE popid=#{brokerid}")
	public abstract int updateBrokerLevel(Map<String, Object> params);
	
	@Select("select * from popuser WHERE popid=#{brokerid}")
	public abstract Map<String,Object> queryBrokerById(int brokerid);
}
